import apiClient from './client';
import { Response } from '../types/response';
import type { UserInfoUpdateType } from '../types/user';


/**
 * 获取当前登录用户信息
 * @returns 用户信息对象
 */
export const getUserInfo = async (): Promise<Response> => {
  try {
    const response: Response = await apiClient.get('/api/user/v1/info');
    return response;
  } catch (error) {
    console.error('获取用户信息接口调用失败:', error);
    throw error;
  }
};


/**
 * 获取用户位置信息
 * @returns 用户位置信息对象
 */
export const getUserLocationApi = async (): Promise<Response> => {
  try {
    const response: Response = await apiClient.get('/api/user/v1/location');
    return response;
  } catch (error) {
    console.error('获取用户位置信息接口调用失败:', error);
    throw error;
  }
};

/**
 * 获取用户位置信息
 * @returns 用户位置信息对象
 */
export const setUserLocationApi = async (
  latitude: number, 
  longitude: number, 
  city: string, 
  address: string, 
  area: string,
  areaCode: string,
  cityCode: string,
): Promise<Response> => {
  try {
    const response: Response = await apiClient.post('/api/user/v1/location', {
      latitude,
      longitude,
      city,
      address,
      area,
      areaCode,
      cityCode,
    });
    return response;
  } catch (error) {
    console.error('获取用户位置信息接口调用失败:', error);
    throw error;
  }
};


/**
 * 修改密码
 * @param oldPassword 旧密码
 * @param newPassword 新密码
 * @returns 操作结果
 */
export const changePassword = async (oldPassword: string, newPassword: string, phone: string = '', code : string=''): Promise<Response> => {
  try {
    return await apiClient.put('/api/auth/v2/password', { 
      old_password:oldPassword,
      new_password:newPassword,
      phone:phone,
      code:code
    });
  } catch (error) {
    console.error('修改密码接口调用失败:', error);
    throw error;
  }
};

/**
 * 修改手机号
 * @param phone 新手机号
 * @param code 验证码
 * @returns 操作结果
 */
export const changePhone = async (phone: string, code: string): Promise<Response> => {
  try {
    return await apiClient.put('/api/auth/v1/phone', {
      new_phone:phone,
      code:code
    });
  } catch (error) {
    console.error('修改手机号接口调用失败:', error);
    throw error;
  }
};


/**
 * 注销账户
 * @returns 操作结果
 */
export const deleteAccount = async (): Promise<any> => {
  try {
    const response: Response = await apiClient.delete('/api/user/v1/account');
    return response;
  } catch (error) {
    console.error('注销账户接口调用失败:', error);
    throw error;
  }
};


/**
 * 更新用户信息
 * @param userInfo 用户信息对象
 * @returns 操作结果
 */
export const updateUserInfoApi = async (userInfo: UserInfoUpdateType): Promise<any> => {

  try {
    return await apiClient.put('/api/user/v1/info', {...userInfo});
  } catch (error) {
    console.error('更新用户信息接口调用失败:', error);
    throw error;
  }
};


export const updateUserSettingApi = async (notifications: boolean, pushMessages: boolean): Promise<any> => {
  try {
    return await apiClient.post('/api/user/v1/settings', {notifications, pushMessages});
  } catch (error) {
    console.error('更新用户设置接口调用失败:', error);
    throw error;
  }
}


export const getUserSettingApi = async (): Promise<any> => {
  try {
    return await apiClient.get('/api/user/v1/settings');
  } catch (error) {
    console.error('更新用户设置接口调用失败:', error);
    throw error;
  }
}


export const sendFeedbackApi = async (feedback: string): Promise<any> => {
  try {
    return await apiClient.post('/api/user/v1/feedback', {
      feedback: feedback
    });
  } catch (error) {
    console.error('发送反馈接口调用失败:', error);
    throw error;
  }
}